use crate::{constants::FIXED_CHUNK_LEN, config::ConfigResult};
use crate::config::error::ConfigError;

pub static mut CHUNKING:fn(&[u8])->usize = FIXED_CHUNKING;

pub const FIXED_CHUNKING:fn(&[u8])->usize = |s|s.len().min(FIXED_CHUNK_LEN);
pub const RABIN_CHUNKING:fn(&[u8])->usize = crate::superfeature::rabin_chunking;
pub const FASTCDC_CHUNKING:fn(&[u8])->usize = crate::superfeature::fastcdc_chunking;

pub fn config_chunking(chunking:&str) -> ConfigResult<()> {
	unsafe {
		match chunking {
			"fixed" => {
				CHUNKING = FIXED_CHUNKING;	
				Ok(())
			},
			"rabin" => {
				CHUNKING = RABIN_CHUNKING;
				Ok(())
			}
			"fastcdc" => {
				CHUNKING = FASTCDC_CHUNKING;
				Ok(())
			}
			"fastcdc_sample" => {
				CHUNKING = FASTCDC_CHUNKING;
				Ok(())
			}
			_ => {
				Err(ConfigError::UnknownChunkingAlgorithm(chunking.to_string()))
			},
		}
	}
}
